#pragma once

#include "Audio/Tests/Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestAudio
#undef  APP_BASE
#define APP_BASE	AudioTestApp


class APP_NAME : public APP_BASE
{
private:
	AudioSamplePtr		sample;


public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{
	}
		
	void Update (float dt) override
	{
		APP_BASE::Update( dt );

		if ( sample.IsNull() )
			return;

		Ptr< Input >	input = ESS()->GetInput();

		if ( input->IsKeyClicked( EKey::P ) )
		{
			DEBUG_CONSOLE( "play" );
			sample->Play();
			DEBUG_CONSOLE( (string("is playing: ") << ( sample->GetState() == EPlayingState::PLAY)).cstr() );
		}

		if ( input->IsKeyClicked( EKey::S ) )
		{
			DEBUG_CONSOLE( "stop" );
			sample->Stop();
			DEBUG_CONSOLE( (string("is stoped: ") << ( sample->GetState() == EPlayingState::STOP)).cstr() );
		}

		if ( input->IsKeyClicked( EKey::H ) )
		{
			DEBUG_CONSOLE( "pause" );
			sample->Pause();
			DEBUG_CONSOLE( (string("is paused: ") << ( sample->GetState() == EPlayingState::PAUSE)).cstr() );
		}

		if ( input->IsKeyClicked( EKey::L ) )
		{
			DEBUG_CONSOLE( "looping" );
			sample->SetLooping( not sample->IsLooping() );
			DEBUG_CONSOLE( (string("is looping: ") << sample->IsLooping()).cstr() );
		}

		if ( input->IsKeyClicked( EKey::M ) )
		{
			DEBUG_CONSOLE( "to center" );
			sample->SetPosition( 0.5f );
		}
		
		if ( input->IsKeyFirstPress( EKey::VOLUME_UP ) )
		{
			float	vol = sample->GetVolume();
			DEBUG_CONSOLE( (string("Volume: ") << vol ).cstr() );
			sample->SetVolume( vol + 0.1f );
		}

		if ( input->IsKeyFirstPress( EKey::VOLUME_DOWN ) )
		{
			float	vol = sample->GetVolume();
			DEBUG_CONSOLE( (string("Volume: ") << vol ).cstr() );
			sample->SetVolume( vol - 0.1f );
		}
	}

	void OnEnter () override
	{
		APP_BASE::OnEnter();
		
		if ( sample.IsNull() )
		{
			DEBUG_CONSOLE( "OnEnter load sound" );
			
			//CHECK( ESS()->GetResourceManager()->Load( EResource::AUDIO_SAMPLE, "sound/mk.mp3", sample ) );
			
			RFilePtr	file;
			CHECK( ESS()->GetFileSystem()->OpenResource( "sound/mk.snd", file ) );

			if ( file.IsNotNull() )
			{
				CHECK( ESS()->GetResourceManager()->Load( EResource::AUDIO_SAMPLE, file,
														sample, AudioSample::GetDefaultLoadParams() ) );
			}
		}
		if ( sample.IsNotNull() )
		{
			sample->SetLooping( true );
			sample->Play();
		}
	}
	
	void OnExit () override
	{
		if ( sample.IsNotNull() )
		{
			DEBUG_CONSOLE( "OnExit, stop playing" );

			sample->Pause();
			sample = null;
		}
		APP_BASE::OnExit();
	}
};
